<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>组件</title>
    <script src='./vue.global.js'></script>
</head>
    <style>
        .app {
            font-family: sans-serif;
            border: 1px solid #eee;
            border-radius: 2px;
            padding: 20px 30px;
            margin-top: 1em;
            margin-bottom: 40px;
            
        }
        .tab-button {
            padding: 6px 10px;
            border-top-left-radius: 3px;
            border-top-right-radius: 3px;
            border: 1px solid #ccc;
            cursor: pointer;
            background: #f0f0f0;
            margin-bottom: -1px;
            margin-right: -1px;
            width: 70px;
            height: 50px;
            display: block;
        }

        .tab-button:hover {
            background: #e0e0e0;
        }

        .tab-button.active {
            background: #e0e0e0;
        }

        .demo-tab {
            border: 1px solid #ccc;
            padding: 10px;
        }
        .root{
            margin-top: 0;
            margin-left: 70px;
            height: 150px;
        }
        .flot{
            float: left;
        }
    </style>
<body>
    <div id='app'>
        <base-input ref="bi"></base-input>
        <br>
        <div class="flot">
            <button v-for="tab in tabs" @click="currentTabComponent = 'tab-'+tab" class="tab-button">{{tab}}</button>
        </div>
        <keep-alive class="root">
            <component :is="currentTabComponent"></component>
        </keep-alive>

        <suspense>
            <template #default>
                <async-com></async-com>
            </template>
            <template #fallback>
                <h1>加载中...</h1>
            </template>
        </suspense>
    </div>

     <script>
        const { createApp, defineAsyncComponent } = Vue

        const app = Vue.createApp({
            data() {
                return {
                    tabs: ["home", "posts", "archive"],
                    currentTabComponent: 'tab-archive'
                }
            },
            methods: {},
            computed: {},

            mounted(){
                console.log("父组件加载完毕");
                this.$refs.bi.focusTheFuckingInput()
            }
        })

        //首页组件
        app.component("tab-home", {
            template: `<div class="demo-tab">首页组件</div>`,
        })

        //博文组件
        app.component("tab-posts", {
            data() {
                return {
                    posts: [
                        {
                            id: 1,
                            title: "薛定谔的猫",
                            content: "Brother Xue's HelloKitty, adipisicing elit. Nihil consequatur debitis a iste officiis nesciunt animi architecto, odit unde quos optio, numquam voluptatibus repellendus fugit porro reprehenderit quae explicabo saepe?"
                        },
                        {
                            id: 2,
                            title: "朕的狗狗",
                            content: "My puppy, adipisicing elit. Nihil consequatur debitis a iste officiis nesciunt animi architecto, odit unde quos optio, numquam voluptatibus repellendus fugit porro reprehenderit quae explicabo saepe?"
                        },
                        {
                            id: 3,
                            title: "碧竹苑的你",
                            content: "Somebody is very beautiful, adipisicing elit. Nihil consequatur debitis a iste officiis nesciunt animi architecto, odit unde quos optio, numquam voluptatibus repellendus fugit porro reprehenderit quae explicabo saepe?"
                        },
                    ],
                    selectedPost: null
                }
            },
            template: ` 
            <div>   
                <ul>
                    <li v-for="post in posts" @click="selectedPost=post">{{post.title}}</li>
                </ul>
                <p>{{selectedPost?selectedPost.content:''}}</p>
            <div>
            `,
        })

        //个人中心组件
        app.component("tab-archive", {
            template: `<div class="demo-tab">个人中心</div>`,
        })


        const AsyncCom = defineAsyncComponent(

            () => new Promise(
                (resolve) => {
                    setTimeout(() => {
                        resolve({
                            template: `<div>fuxingmou传奇</div>`
                        })
                    }, 2000);
                }
            )

        )

        app.component("async-com",AsyncCom)
        
        app.component("base-input",{
            template:`<input type="text" ref="ip">`,

            mounted(){
                console.log("子组件加载完毕");
            },

            methods:{
                focusTheFuckingInput(){
                    this.$refs.ip.focus()
                }
            }
        })

        app.mount('#app')
    </script>
</body>

</html>